import Vue from 'vue'
import axios from 'axios'
// import store from '@/vuex/store'
import { VueAxios } from './axios'
import { Dialog, Notify } from 'vant'
import { ACCESS_TOKEN, TENANT_ID } from '@/vuex/mutation-types'

/**
 * 【指定 axios的 baseURL】
 * 如果手工指定 baseURL: '/jeecg-boot'
 * 则映射后端域名，通过 vue.config.js
 * @type {*|string}
 */
//拓天企业微信地址
// let apiBaseUrl = 'http://topfluid.vicp.net:28891/ttdnJK-2.4.2'
// let apiBaseUrl1 = 'http://qywx.topfluid.com:8083/saleJK' //获取企业微信接口
//测试拓天企业微信地址
let apiBaseUrl = 'http://topfluid.vicp.net:28891/ttdnJK-2.4.2'
let apiBaseUrl1 = 'http://mes.baitiansoft.com:8083/saleJK1' //获取企业微信接口

// 创建 axios 实例
const service = axios.create({
  baseURL: apiBaseUrl, // api base_url
  timeout: 9000 // 请求超时时间
})
const service1 = axios.create({
  baseURL: apiBaseUrl1, // api base_url
  timeout: 9000 // 请求超时时间
})

const err = (error) => {
  if (error.response) {
    let data = error.response.data
    const token = Vue.ls.get(ACCESS_TOKEN)
    // console.log('------异常响应------', token)
    switch (error.response.status) {
      case 403:
        Notify({ type: 'danger', message: '系统提示：拒绝访问', duration: 4000 })
        break
      case 500:
        if (!token || data.message.indexOf('Token失效，请重新登录') != -1) {
          Dialog.alert({
            title: '登录已过期',
            message: '很抱歉，登录已过期，请重新登录',
            confirmButtonText: '重新登录'
          }).then(() => {
            Vue.ls.remove(ACCESS_TOKEN)
            window.location.hash = '/'
            // store.dispatch('reLogin')
          })
        }
        break
      case 404:
        Notify({ type: 'danger', message: '系统提示：很抱歉，资源未找到!', duration: 4000 })
        break
      case 504:
        Notify({ type: 'danger', message: '系统提示：网络超时!', duration: 4000 })
        break
      case 401:
        Notify({ type: 'danger', message: '系统提示：未授权，请重新登录!', duration: 4000 })
        if (token) {
          Dialog.alert({
            title: '登录已过期',
            message: '很抱歉，未授权，请重新登录',
            confirmButtonText: '重新登录'
          }).then(() => {
            Vue.ls.remove(ACCESS_TOKEN)
            window.location.hash = '/'
          })
        }
        break
      default:
        Notify({ type: 'danger', message: '系统提示' + data.message + '!', duration: 4000 })
        break
    }
  }
  return Promise.reject(error)
}

// request interceptor
service.interceptors.request.use(config => {
  const token = Vue.ls.get(ACCESS_TOKEN)
  if (token) {
    config.headers[ 'X-Access-Token' ] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  }
  // update-begin-author:taoyan date:2020707 for:多租户
  let tenantid = Vue.ls.get(TENANT_ID)
  if (!tenantid) {
    tenantid = 0
  }
  config.headers[ 'tenant_id' ] = tenantid
  // update-end-author:taoyan date:2020707 for:多租户
  if (config.method === 'get') {
    if (config.url.indexOf('sys/dict/getDictItems') < 0) {
      config.params = {
        _t: Date.parse(new Date()) / 1000,
        ...config.params
      }
    }
  }
  return config
}, (error) => {
  return Promise.reject(error)
})

// response interceptor
service.interceptors.response.use((response) => {
  return response.data
}, err)

// request interceptor
service1.interceptors.request.use(config => {
  const token = Vue.ls.get(ACCESS_TOKEN)
  if (token) {
    config.headers[ 'X-Access-Token' ] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  }
  // update-begin-author:taoyan date:2020707 for:多租户
  let tenantid = Vue.ls.get(TENANT_ID)
  if (!tenantid) {
    tenantid = 0
  }
  config.headers[ 'tenant_id' ] = tenantid
  // update-end-author:taoyan date:2020707 for:多租户
  if (config.method === 'get') {
    if (config.url.indexOf('sys/dict/getDictItems') < 0) {
      config.params = {
        _t: Date.parse(new Date()) / 1000,
        ...config.params
      }
    }
  }
  return config
}, (error) => {
  return Promise.reject(error)
})

// response interceptor
service1.interceptors.response.use((response) => {
  return response.data
}, err)
const installer = {
  vm: {},
  install (Vue, router = {}) {
    Vue.use(VueAxios, router, service)
  }
}

export {
  installer as VueAxios,
  service as axios,
  service1 as axios1,
  apiBaseUrl as apiBaseUrl
}
